<?php
declare (strict_types = 1);
namespace app\admin\controller;
use app\common\controller\Backend;
use think\facade\View;

class Adboard extends Backend
{
    public function initialize() {
        parent::initialize();
        $this->_mod  = new \app\admin\model\Adboard();
    }

    public function _before_index() {
        $where = array();
        $keyword =  request()->param('keyword');
        if($keyword) $where[]=array('name','like','%'.$keyword.'%');
        View::assign('search', array(
            'keyword'=>$keyword,
        ));
        $this->_list($this->_mod,$where, 'id', 'desc',0);
        $tpl_list = $this->_mod->get_tpl_list();
        View::assign('tpl_list', $tpl_list);
    }

    public function _before_add() {
        $tpl_list = $this->_mod->get_tpl_list();
        View::assign('tpl_list', $tpl_list);
    }

    public function _before_insert($data) {
        if ($this->_mod->name_exists($data['name'])) {
            return '广告位名称已存在';
        }
    }

    public function _before_edit() {
        $tpl_list = $this->_mod->get_tpl_list();
        View::assign('tpl_list', $tpl_list);
    }

    public function _before_update($data) {
        if ($this->_mod->name_exists($data['name'], $data['id'])) {
            return '广告位名称已存在';
        }
    }
}


